Skip to main content

Create Table

Creating a New Table

When creating a new table in the database, you can define various columns with different properties, such as data types, constraints, and default values. Below are a few examples showcasing how to use the table creation UI.

Table Name and Description

  • Table Name: The name of the table is entered at the top of the form (e.g., admin).
  • Description: You can provide a description of the table, explaining its purpose.

Example 1: Creating a Basic Table

Here’s an example of a simple table definition for a users table:

  • Table Name: users
  • Description: Stores basic information of users.
Column NameData TypePropertiesCommentsDefault Value
idintPrimary Key, Auto Increment, Not NullUnique identifier for the user
usernamevarchar(50)Not NullUser’s login name
emailvarchar(100)Unique, Not NullUser’s email address
password_hashvarchar(255)Not NullHashed password for security
created_attimestampNot NullAccount creation timestampCURRENT_TIMESTAMP
  • + Add Column: Add additional columns for more user information.
  • + Add Index: You can add an index on the email column to optimize searches for user email addresses.

Properties

  • Primary Key: The id column is marked as the primary key.
  • Auto Increment: The id is auto-incremented for each new row.
  • Not Null: Ensures that all important fields (e.g., username, email, and password_hash) cannot be empty.

Example 2: Creating an Orders Table

Here’s an example of a table that stores orders information:

  • Table Name: orders
  • Description: Stores order information for the e-commerce platform.
ColumnNameData TypePropertiesCommentsDefault Value
order_idintPrimary Key, Auto Increment, Not NullUnique identifier for eachorder
user_idintForeign Key (references users.id), Not NullLinks order to a user
total_amountdecimal (10,2)Not NullTotal cost of the order
order_statusvarchar (20)Not NullStatus of the orderpending
order_datetimestampNot NullDate and time of order creationCURRENT_TIMESTAMP
shipping_datetimestampNullExpected shipping date

Properties

  • Primary Key: The order_id column is marked as the primary key.
  • Foreign Key: The user_id column references the id column in the users table.
  • Not Null: Key fields like user_id, total_amount, order_status, and order_date are mandatory.
  • Default Value: The order_status is set to pending by default when a new order is created.

Adding an Index

  • Index on user_id: You can add an index on user_id to optimize the retrieval of orders for a particular user.
  • Composite Index: A composite index on user_id and order_date can be added to optimize searching for orders by a specific user within a certain date range.

Example 3: Creating a Products Table with Different Data Types

Here’s an example of a table that stores product information:

  • Table Name: products
  • Description: Stores product details for an e-commerce platform.
Column NameData TypePropertiesCommentsDefault Value
product_idintPrimary Key, Auto Increment, Not NullUnique identifier for each product
product_namevarchar(100)Not NullName of the product
descriptiontextNullDescription of the product
pricedecimal(10,2)Not NullPrice of the product
stock_quantityintNot NullAvailable stock quantity0
category_idintForeign Key (references categories.category_id)Category to which the product belongs

Properties

  • Primary Key: The product_id column is marked as the primary key.
  • Foreign Key: The category_id column references the category_id in the categories table.
  • Not Null: Ensures that product_name, price, and stock_quantity fields must have values.
  • Default Value: The stock_quantity defaults to 0.

Example 4: Creating a Categories Table with Nested Indexes

Here’s an example of a table that stores product categories with index management:

  • Table Name: categories
  • Description: Stores category information for products.
Column NameData TypePropertiesCommentsDefault Value
category_idintPrimary Key, Auto Increment, Not NullUnique identifier for category
category_namevarchar(100)Not NullName of the category
parent_idintForeign Key (self-reference), NullReferences parent category ID

Properties

  • Primary Key: The category_id column is marked as the primary key.
  • Foreign Key: The parent_id column is a self-referencing foreign key, allowing a category to have a parent category.
  • Not Null: The category_name field must have a value.

Adding Indexes

  • Composite Index: You can add a composite index on category_id and parent_id to improve the performance of hierarchical category queries.

Summary

The table creation process in this UI allows users to define columns, data types, properties, and default values with ease. Indexes can be added to columns to improve query performance, and relationships can be defined between tables using foreign keys. The intuitive interface simplifies the creation and management of database tables.